package cp2013;

import java.util.ArrayList;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.JPanel;

/**
 *
 * @author AcerXPS
 */
public class HomeUI extends JPanel {

    private HomePanel homePanel;
    private UserPanel userPanel;
    private JScrollPane scrollPanel;
    private ArrayList<ItemPanel> itemPanelArr;
    private ArrayList<Item> itemArr;
    private JPanel objectWindow;

    public HomeUI() {
        homePanel = new HomePanel();
        userPanel = new UserPanel();
    }

    public JPanel getObjectWindow() {
        return objectWindow;
    }

    public void setItemArr(ArrayList itemArr) {
        this.itemArr = itemArr;
    }

    public void initiateHomeUI() {
        setLayout(new BorderLayout());
        homePanel.initiateHomePanel();
        userPanel.loadPanel();

        this.createItemPanels();

        add(BorderLayout.EAST, homePanel);
        add(BorderLayout.NORTH, userPanel);
    }
    

    public void addHomePanelListener(ActionListener listener) {
        homePanel.addHomePanelListener(listener);
    }

    public void addBidButtonListener(ActionListener listener) {
        for (int i = 0; i < itemPanelArr.size(); i++) {
            itemPanelArr.get(i).addItemPanelListener(listener);
        }
    }

    public HomePanel getPanel() {
        return homePanel;
    }

    public void setUserPanel(UserPanel userPanel) {
        this.userPanel = userPanel;
    }

    public UserPanel getUserPanel() {
        return userPanel;
    }

    public ArrayList getArrayList() {
        return itemPanelArr;
    }

    public void createItemPanels() {
        itemPanelArr = new ArrayList();
        objectWindow = new JPanel();
        objectWindow.setLayout(new BoxLayout(objectWindow, BoxLayout.Y_AXIS));


        for (int i = 0; i < itemArr.size(); i++) {
            itemPanelArr.add(new ItemPanel(itemArr.get(i)));
            objectWindow.add(itemPanelArr.get(i));
        }
        scrollPanel = new JScrollPane(objectWindow);

        scrollPanel.setAutoscrolls(true);
        scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        
        add(BorderLayout.CENTER, scrollPanel);
    }

    public void refreshScrollPanel(){
        this.remove(scrollPanel);
    }
}
